package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.member.service.IUserInfoService;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import cn.edu.gduf.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户注册登录
 */
@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    /**
     * 检查手机号码是否被注册过
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public boolean get(String phone) {
        return userInfoService.checkPhone(phone);
    }

    /**
     * 发送短信验证码
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    /**
     * 用户注册
     */
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object register(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.register(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.SUCCESS;
    }

    /**
     * 用户登录操作
     */
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletResponse response, String username, String password) {
        String token = userInfoService.userLogin(username, password);
        CookieUtil.addCookie(response,token);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/checkLogin")
    @ResponseBody
    public Object checkLogin(@UserParam UserInfo userInfo){
        return userInfo != null ? AjaxResult.SUCCESS : AjaxResult.FAIL;
    }

}
